<?php

namespace App\Utils;

class AES
{
    /**
     * CI_Aes cipher
     * @var string
     */
    protected $cipher = 'aes-128-ecb';
    /**
     * CI_Aes key
     *
     * @var string
     */
    protected $key;
    /**
     * CI_Aes constructor
     * @param string $key Configuration parameter
     */

    public function __construct($key=null){
        $this->key = $key;
    }

    /**
     * Initialize
     *
     * @param array $params Configuration parameters
     * @return CI_Encryption
     */
    public function initialize($params)
    {
        if (!empty($params) && is_array($params)) {
            foreach ($params as $key => $val) {
                $this->$key = $val;
            }
        }
    }
    /**
     * Encrypt
     *
     * @param string $data Input data
     * @return string
     */
    public function encrypt($data) {
        $endata =  openssl_encrypt($data, $this->cipher, $this->key, OPENSSL_RAW_DATA);
        return  bin2hex($endata);
    }

    /**
     * Decrypt
     *
     * @param string $data Encrypted data
     * @return string
     */
    public function decrypt($data) {
        $encrypted = hex2bin($data);
        return openssl_decrypt($encrypted, $this->cipher, $this->key, OPENSSL_RAW_DATA);
    }
}
